package pe.gob.mpfn.controller.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.jsf.converter.RooJsfConverter;
import pe.gob.mpfn.model.Personal;
import pe.gob.mpfn.service.PersonalService;

@Configurable
@FacesConverter("pe.gob.mpfn.controller.converter.PersonalConverter")
@RooJsfConverter(entity = Personal.class)
public class PersonalConverter implements Converter  {

	@Autowired
    PersonalService personalService;

	public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        Integer id = Integer.parseInt(value);
        return personalService.findPersonal(id);
    }

	public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value instanceof Personal ? ((Personal) value).getIntPersonalId().toString() : "";
    }
}
